//
//  PhotoBrowerCollectionViewCell.swift
//  swift-GWYWeiBo
//
//  Created by x on 2017/6/30.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

protocol PhotoBrowerCollectionViewCellDelegate: NSObjectProtocol {
    func photoBrowerCollectionViewCellDidClose(cell: PhotoBrowerCollectionViewCell)
}


class PhotoBrowerCollectionViewCell: UICollectionViewCell {

    
    /// 代理对象
    weak var photoBrowerDelegate:PhotoBrowerCollectionViewCellDelegate?
    
    /// 图片的URL
    var photoImageURL: URL? {
        didSet {
            //photoImage.sd_setImage(with: photoImageURL!)
            /*
             1.图片显示不完全
             2.图片没有剧中显示
             3.长图显示也会有一些问题
             */
            photoImage.sd_setImage(with: photoImageURL!) { (image, _, _, _) in
                /*
                let size = self.displaySize(image: image!)
                self.photoImage.frame = CGRect(origin: CGPoint.zero, size: size)
                
                //处理居中显示
                let y = (UIScreen.main.bounds.size.height - size.height)/2
                self.scroller.contentInset = UIEdgeInsetsMake(y, 0, y, 0)
                */
                //调整图片的位置
                self.setImageViewPostion(image: image!)
            }
        }
    }
    
    /// 每次电视图片时候,要重置ScrollView和image的一些属性
    private func reset() ->Void {
    
        //1.重置ScrollView
        scroller.contentInset = UIEdgeInsets.zero
        scroller.contentSize = CGSize.zero
        scroller.contentOffset = CGPoint.zero
        
        //2.重置imageView
        photoImage.transform = CGAffineTransform.identity
    }
    
    /// 显示图片
    ///
    /// - Parameter image: <#image description#>
    private func setImageViewPostion(image: UIImage) ->Void {
        //重置
        reset()
        
        //1.拿到安宽高比计算后的图片大小
        let size = displaySize(image: image)
        
        //2.判断图片的高度是够大于屏幕的高度
        let height = UIScreen.main.bounds.size.height
        photoImage.frame = CGRect(origin: CGPoint.zero, size: size)
        if size.height > height{
            //2.1 大于 长图 -> y=0 设置scrollView的滚动大小等于图片大小
            scroller.contentSize = size
        }else {
            //2.2 小于 短图 -> 设置contentInset属性 让图片居中显示
            let y = (UIScreen.main.bounds.size.height - size.height)/2
            scroller.contentInset = UIEdgeInsetsMake(y, 0, y, 0)
        }
    }
    
    /// 按照图片的宽高比计算图片的显示大小
    ///
    /// - Parameter image: <#image description#>
    /// - Returns: <#return value description#>
    private func displaySize(image: UIImage) -> CGSize {
        
        // 1.得到高宽比
        let scale = image.size.height / image.size.width
        let width = UIScreen.main.bounds.size.width
        
        //2.计算图片的高度
        let height = width * scale
        
        return CGSize(width: width, height: height)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    private func setupUI() -> Void {
        //1.添加子控件
        contentView.addSubview(scroller)
        scroller.addSubview(photoImage);
        
        //2.布局子控件
        scroller.frame = bounds
        
        //3.设置缩放
        scroller.delegate = self
        scroller.minimumZoomScale = 0.5
        scroller.maximumZoomScale = 2.0
        
        //4.监听图片
        photoImage.isUserInteractionEnabled = true
        let tapGR = UITapGestureRecognizer(target: self, action: #selector(close))
        photoImage.addGestureRecognizer(tapGR)
        
    }

    
    /// 关闭图片浏览控制器
    func close() -> Void {
        
        //通知控制器dismiss
        photoBrowerDelegate?.photoBrowerCollectionViewCellDidClose(cell: self)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: -- 懒加载
    
    /// 图片
    lazy var photoImage: UIImageView = UIImageView();
    
    /// 加载ScrollView
    private lazy var scroller: UIScrollView = UIScrollView()

}

extension PhotoBrowerCollectionViewCell: UIScrollViewDelegate {

    /// 告诉系统你要缩放那个控件
    ///
    /// - Parameter scrollView: <#scrollView description#>
    /// - Returns: <#return value description#>
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {

        return photoImage
    }
    
    
    /// 缩放结束后执行方法
    ///
    /// - Parameters:
    ///   - scrollView: <#scrollView description#>
    ///   - view: <#view description#>
    ///   - scale: <#scale description#>
    func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
        
        //ScrollView在缩放的时候,只改变frame ,bounce是不会改变的
        //缩放的本质还是修改了transform ,但是修改transform是不会影响bounces的 会影响frame
//        print(view?.frame ?? 0)
//        print(view?.bounds ?? 0)
        //1.缩放之后改变ScrollView的四边间距
        let top = (UIScreen.main.bounds.height - (view?.frame.height)!)/2
        let left = (WIDTH - (view?.frame.width)!)/2
        //调整ScrollView内部的内容距离ScrollView四边的距离
        scrollView.contentInset = UIEdgeInsetsMake(top, left, top, left)
    }
}






